package com.wm.machine.baselibrary.utils.imageutils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;

import java.io.IOException;

/**
 * Copyright
 * author zhengcheng
 * date 2018/2/11
 * describe 图片处理相关工具类
 * history
 */
public class ImageUtil {

  private static final int ROTATE_90 = 90;
  private static final int ROTATE_180 = 180;
  private static final int ROTATE_270 = 270;

  protected ImageUtil() {
    throw new UnsupportedOperationException();
  }

  /**
   * 计算图片返回的压缩比例
   * 
   * @param options 目标图片的配置参数
   * @param reqWidth 压缩后图片的宽度
   * @param reqHeight 压缩后图片的高度
   * @return 压缩比例
   */
  private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth,
      int reqHeight) {
    //  源图片的高度和宽度  
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
      //  计算出实际宽高和目标宽高的比率  
      final int heightRatio = Math.round((float) height / (float) reqHeight);
      final int widthRatio = Math.round((float) width / (float) reqWidth);
      //  选择宽和高中最小的比率作为inSampleSize的值，这样可以保证最终图片的宽和高  
      //  一定都会大于等于目标的宽和高。  
      inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
    }
    return inSampleSize;
  }

  /**
   * 返回图片压缩后的bitmap
   * 
   * @param path 目标图片的路径
   * @param reqWidth 压缩后图片的宽度
   * @param reqHeight 压缩后图片的高度
   * @return 图片压缩后的bitmap
   */
  public static Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight) {
    //  第一次解析将inJustDecodeBounds设置为true，来获取图片大小  
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    //  调用上面定义的方法计算inSampleSize值  
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    //  使用获取到的inSampleSize值再次解析图片  
    options.inJustDecodeBounds = false;
    return rotateByDegree(BitmapFactory.decodeFile(path, options), qryRotateDegree(path));
  }

  /**
   * 将图片按照某个角度进行旋转
   * 
   * @param source 需要旋转的图片
   * @param degree 旋转角度
   * @return 旋转后的图片
   */
  private static Bitmap rotateByDegree(Bitmap source, int degree) {
    Bitmap temBitmap = source;
    if (0 != degree) {
      try {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        temBitmap =
            Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
        source.recycle();
      } catch (OutOfMemoryError error) {
        error.printStackTrace();
      }
    }
    return temBitmap;
  }

  /**
   * 读取图片的旋转的角度
   * 
   * @param path 图片
   * @return 图片的旋转角度
   */
  private static int qryRotateDegree(String path) {
    int degree = 0;
    try {
      ExifInterface exifInterface = new ExifInterface(path);
      int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
          ExifInterface.ORIENTATION_NORMAL);
      switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
          degree = ROTATE_90;
          break;
        case ExifInterface.ORIENTATION_ROTATE_180:
          degree = ROTATE_180;
          break;
        case ExifInterface.ORIENTATION_ROTATE_270:
          degree = ROTATE_270;
          break;
        default:
          degree = 0;
          break;
      }
    } catch (IOException exception) {
      exception.printStackTrace();
    }
    return degree;
  }
}
